package com.qk.controller;

import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.entity.PageResult;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @PostMapping("/depts" )
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门：{}",dept);
        deptService.add(dept);
        return Result.success();

    }
    @GetMapping("/depts" )
    public Result list(String  name, Integer status ,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<Dept> pageResult = deptService.list(name,status,page,pageSize);
        log.info("查询部门列表：{}",pageResult);
        return Result.success(deptService.list(name, status, page, pageSize));
    }
    @GetMapping("/depts/{id}" )
    public Result getByID(@PathVariable Integer id) {
        System.out.println("Id查询" +id);
        return Result.success(deptService.getByID(id));
    }
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success();
    }
    @DeleteMapping("/depts/{id}" )
    public Result delete(@PathVariable Integer id) {
        deptService.delete(id);
        return Result.success();
    }
    @GetMapping("/depts/list")
    public Result list() {
        return Result.success(deptService.getAll());
    }
}
